package yagwl.client.widget;

import yagwl.client.widget.internal.YClientBundle;
import yagwl.client.widget.internal.YCssResource;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class SimpleFormPanel extends Composite {
  private static final YCssResource ycss = YClientBundle.I.css();
  private final FlowPanel rootFlowPanel = new FlowPanel();
  private final FlexTable fieldsTable = new FlexTable();
  private final Button button = new Button();
  private final Image busyI = new Image(YClientBundle.I.inlineBusy());
  private final Label messageL = new Label();
  private final Image hideI = new Image(YClientBundle.I.inlineDelete());
  private final ActionTimer timer = new ActionTimer();
  private Prompt prompt;

  public SimpleFormPanel(Prompt prompt) {
    this.prompt = prompt;
    HorizontalPanel hPanel = new HorizontalPanel();
    hPanel.add(button);
    hPanel.add(busyI);
    hPanel.add(messageL);
    
    rootFlowPanel.add(hideI);
    rootFlowPanel.add(fieldsTable);
    rootFlowPanel.add(hPanel);
    initWidget(rootFlowPanel);

    busyI.setVisible(false);
    hideI.setVisible(false);
    busyI.setStyleName(ycss.simpleFormPanelBusy());
  }

  public void addField(Widget inputWidget, boolean required) {
    fieldsTable.setWidget(fieldsTable.getRowCount(), 0, inputWidget);
  }

  public void addField(String label, Widget inputWidget, boolean required) {
    switch (prompt) {
    case None:
      fieldsTable.setWidget(fieldsTable.getRowCount(), 0, inputWidget);
      break;
    case Top:
      fieldsTable.setText(fieldsTable.getRowCount(), 0, label);
      fieldsTable.setWidget(fieldsTable.getRowCount(), 0, inputWidget);
      break;
    case Left:
      fieldsTable.setText(fieldsTable.getRowCount(), 0, label);
      fieldsTable.setWidget(fieldsTable.getRowCount() - 1, 1, inputWidget);
    }
  }

  public void setButton(String label, ClickHandler clickHandler) {
    button.setText(label);
    button.addClickHandler(clickHandler);
    button.setStyleName(ycss.simpleFormPanelButton());
  }

  public void setCancelAction(String label, ClickHandler clickHandler) {
    hideI.setVisible(true);
    hideI.setTitle("Cancel");
    hideI.setStyleName(ycss.simpleFormPanelCancel());
    hideI.addClickHandler(clickHandler);
  }

  public void setBusy(boolean busy) {
    busyI.setVisible(busy);
    button.setEnabled(!busy);
    messageL.setText("");
  }

  public void setMessage(String message) {
    messageL.setStyleName(YClientBundle.I.css().simpleFormPanelMessage());
    setStatus(message, 8);
  }

  public void setErrorMessage(String message) {
    messageL.setStyleName(YClientBundle.I.css().simpleFormPanelErrorMessage());
    setStatus(message, 5);
  }

  private void setStatus(String message, int displaySeconds) {
    setBusy(false);
    messageL.setText(message);
    if (displaySeconds > 0) {
      timer.cancel();
      timer.schedule(displaySeconds * 1000);
    }
  }

  public enum Prompt {
    None, Top, Left
  }

  private class ActionTimer extends Timer {

    @Override
    public void run() {
      messageL.setText("");
    }

  }

}
